﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using ClassLibrary.Data;
using ClassLibrary.DAO;
using System.IO;

public partial class Admin_ucControl_ucAddItems : System.Web.UI.UserControl
{
    TblItemsDAO itemDAO = new TblItemsDAO(Global.CONNECTION_STRING);
    TblMenuChildDAO menuChilđAO = new TblMenuChildDAO(Global.CONNECTION_STRING);
    TblImageDAO imgDAO = new TblImageDAO(Global.CONNECTION_STRING);

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadMenuChild();
            if (rdNo.Checked)
            {
                lbGiaKhuyenmai.Enabled = false;
                txtmakhuyenmai.Enabled = false;
                txtPricekhuyenmai.Enabled = false;
                lbmakhuyenmai.Enabled = false;
            }
            else
            {
                lbGiaKhuyenmai.Enabled = true;
                txtmakhuyenmai.Enabled = true;
                txtPricekhuyenmai.Enabled = true;
                lbmakhuyenmai.Enabled = true;
            }

            string id = Request.QueryString["id"];
            if (Global.ConvertToInt(id) != 0)
            {
                LoadDetail();
              //  LoadImage();
            }
            if (Session["ImageView"] != null)
            {
                lbImageView.Text = Session["ImageView"].ToString();
            }
        }
    }

    private void LoadMenuChild()
    {
        drLoaiSP.DataSource = menuChilđAO.GetAllMenuChild();
        drLoaiSP.DataValueField = "IDMenuChild";
        drLoaiSP.DataTextField = "MenuChildName";
        drLoaiSP.DataBind();
    }

    private void LoadDetail()
    {
        string id = Request.QueryString["id"];
        LoadMenuChild();
        TblItems item = itemDAO.GetItemByID(Global.ConvertToInt(id));
        txtKhuyenMai.Text = item.KhuyenMai;
        txtTenSP.Text = item.ItemName;
        txtPrice.Text = Convert.ToDecimal(item.Price).ToString();
        txtPricekhuyenmai.Text = Convert.ToDecimal(item.PriceTaxCode).ToString();
        txtmakhuyenmai.Text = Convert.ToInt32(item.Discount).ToString();
        if (item.PriceTaxCode > 0)
        {
            rdNo.Checked = false;
            rdYes.Checked = true;
        }
        else
        {
            rdNo.Checked = true;
            rdYes.Checked = false;
        }

        description.Value = item.Description;
        LoadImage();

    }
    protected void txtmakhuyenmai_TextChanged(object sender, EventArgs e)
    {
        double khuyenmai = 0;
        khuyenmai = double.Parse(txtPrice.Text) * double.Parse(txtmakhuyenmai.Text) / 100;
        txtPricekhuyenmai.Text = (double.Parse(txtPrice.Text) - khuyenmai).ToString();
    }
    protected void btnLuu_Click(object sender, EventArgs e)
    {
        bool flag = false;
        try
        {
            string id = Request.QueryString["id"];
            TblItems item = new TblItems();
            item.ItemName = txtTenSP.Text;
            item.IDMenuChild = Global.ConvertToInt(drLoaiSP.SelectedValue);
            item.KhuyenMai = txtKhuyenMai.Text;
            item.Price = Convert.ToDecimal(txtPrice.Text);
            if (rdNo.Checked == true)
            {
                item.Discount = 0;
                item.PriceTaxCode = 0;
            }
            else
            {
                item.Discount = Global.ConvertToInt(txtmakhuyenmai.Text);
                item.PriceTaxCode = Convert.ToDecimal(txtPricekhuyenmai.Text);
            }

            item.Vote = 239;
            item.Type = Global.ConvertToInt(drType.SelectedValue);
            item.Description = description.Value;

            if (id == "" || id == null)
            {
                itemDAO.InsUp(item, 1);
            }
            else
            {
                item.ItemID = Global.ConvertToInt(id);
                itemDAO.InsUp(item, 2);
            }

            flag = true;
            
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            Response.Redirect("~/Admin/Default.aspx");
        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        txtKhuyenMai.Text = txtPrice.Text = txtTenSP.Text = txtPricekhuyenmai.Text = txtmakhuyenmai.Text = string.Empty;
    }
    protected void btnDelImg_Click(object sender, ImageClickEventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            TblImage imgItem = new TblImage();

            imgItem.IDImage = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            int idXoa = Convert.ToInt32(((ImageButton)sender).CommandArgument);
            imgItem = imgDAO.GetItemImageByID(Global.ConvertToInt(id), idXoa);
            imgDAO.DeleteImgItem(imgItem);

            string filename = imgItem.ImageURL;
            if (File.Exists(Server.MapPath("~\\Admin\\img\\Items\\" + filename)))
            {
                File.Delete(Server.MapPath("~\\Admin\\img\\Items\\" + filename));
            }

            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            LoadImage();
        }
    }
    protected void btnUploadImg_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"];
        bool flag = false;
        try
        {
            // Save Image
            TblImage img = new TblImage();
            string path = Server.MapPath("~\\Admin\\img\\Items\\");
            string filename = imgURL.FileName;
            if (System.IO.File.Exists(path + filename))
            {

                filename = DateTime.Now.ToString("mm") + "_" + DateTime.Now.ToString("ss") + "_" + filename;
            }
            else
            {
                filename = filename.ToString();
            }
            imgURL.SaveAs(path + filename);
            Session["filename"] = filename;
            img.ImageURL = Session["filename"].ToString();

            if (lbImageView.Text != "")
            {
                string listimageview = Session["ImageView"].ToString();
                img.ImageDetail = listimageview;
            }
            else
            {
                img.ImageDetail = filename;
            }

            img.ItemID = Global.ConvertToInt(id);
            imgDAO.TblImage_Ins(img,1);
            flag = true;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        if (flag)
        {
            Session["ImageView"] = null;
            Session["filename"] = null;
            LoadImage();
        }
    }

    private void LoadImage()
    {
        string id = Request.QueryString["id"];
        grdImgItems.DataSource = imgDAO.GetImage_ByItemID(Global.ConvertToInt(id));
        grdImgItems.DataBind();
    }
}
